package com.lu.manage.core.shiro.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lu.manage.core.common.enums.YesOrNotEnum;
import com.lu.manage.core.shiro.ShiroKit;
import com.lu.manage.core.shiro.ShiroUser;
import com.lu.manage.core.shiro.service.UserAuthService;
import com.lu.manage.core.utils.ToolUtil;
import com.lu.manage.modular.system.model.Role;
import com.lu.manage.modular.system.model.User;
import com.lu.manage.modular.system.service.MenuService;
import com.lu.manage.modular.system.service.RoleService;
import com.lu.manage.modular.system.service.UserService;
import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program LuBoot
 * @description:
 * @author: zhanglu
 * @create: 2019-10-08 17:18:00
 */
@Service
@Transactional(readOnly = true)
public class UserAuthServiceImpl implements UserAuthService {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Override
    public User user(String account) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("account", account);
        User user = userService.getOne(queryWrapper);
        return user;
    }

    @Override
    public ShiroUser shiroUser(User user) {

        // 账号不存在
        if (null == user) {
            throw new CredentialsException();
        }
        // 账号被冻结
        if (user.getStatus() == YesOrNotEnum.N.getCode()) {
            throw new LockedAccountException();
        }

        ShiroUser shiroUser = ShiroKit.createShiroUser(user);
        //用户角色数组
        String[] roleArray = Convert.toStrArray(user.getRoleId());

        List<String> roleList = new ArrayList<>();
        List<String> roleNameList = new ArrayList<>();
        for (String roleId : roleArray) {
            roleList.add(roleId);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("role_id", roleId);
            queryWrapper.select("name");
            Role role = roleService.getBaseMapper().selectOne(queryWrapper);
            roleNameList.add(role.getName());
        }
        shiroUser.setRoleList(roleList);
        shiroUser.setRoleNames(roleNameList);
        return shiroUser;
    }

    @Override
    public List<String> findPermissionsByRoleId(String roleId) {
        List<String> permissionsByRoleId = menuService.findPermissionsByRoleId(roleId);
        return permissionsByRoleId;
    }

    @Override
    public String findRoleNameByRoleId(String roleId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("name");
        queryWrapper.eq("role_id", roleId);
        Role role = roleService.getBaseMapper().selectOne(queryWrapper);
        return ToolUtil.isEmpty(role) ? "未知角色" : role.getName();
    }

}
